package com.chat.client.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chat.client.control.ChatSender;
import com.chat.client.control.IChatProtocal;

public class ChatSample extends Activity {
	
	public static final String TAG = ChatSample.class.getSimpleName();
	
	public static final String SERVER_FAIL = "서버와 연결 할 수 없습니다.\n관리자에게 문의 하세요.";
	
	private ChatSender chatSender;
	private TextView chatView;
	private TextView idView;
	private String nickName;
	private EditText chatContents;
	
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case IChatProtocal.MESSAGE_TYPE_CHAT:
				String message = (String)msg.obj;
				chatContents.append(message+"\n");
				break;
			}
		}
	};
	
    OnClickListener onClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch(v.getId()) {
			case R.id.connection:
				nickName = idView.getText()+"";
				chatConnection(nickName);
				idView.setClickable(false);
				break;
			case R.id.exit:
				chatOut(nickName);
				idView.setClickable(true);
				break;
			case R.id.send:
				String message = chatView.getText()+"";
				if(message != null && !message.trim().equals(""))sendMessage(nickName, message);
				break;
			}
		}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        chatContents = (EditText)findViewById(R.id.chat_contents);
        chatContents.setTextColor(Color.WHITE);
        
        ((Button)findViewById(R.id.connection)).setOnClickListener(onClickListener);
        ((Button)findViewById(R.id.exit)).setOnClickListener(onClickListener);
        ((Button)findViewById(R.id.send)).setOnClickListener(onClickListener);
        
        idView = (TextView)findViewById(R.id.con_id);
        chatView = (TextView)findViewById(R.id.chat_input);
    }
    
    // 메시지 보내기
	public void sendMessage(String nickName, String message) {
		if(chatSender == null)toastMessage(SERVER_FAIL);
    	chatSender.sendMessage(nickName, message);
    }
    
	// 채팅방 입장
    public void chatConnection(String nickName) {
    	chatSender = chatSenderCreate("192.168.1.124", 7777, handler);
    	if(chatSender == null)toastMessage(SERVER_FAIL);
    	chatSender.chatEntry(nickName);
    }
    
    // 채팅 서버 나오기
    public void chatOut(String nickName) {
    	if(chatSender == null)toastMessage(SERVER_FAIL);
    	chatSender.chatOut(nickName);
    }
    
    // 채팅 서버 연결
    private ChatSender chatSenderCreate(String ipaddress, int port, Handler handler) {
        try {
			return new ChatSender(ipaddress, port, handler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    }
    
    private void toastMessage(String msg) {
    	Toast.makeText(this, msg, Toast.LENGTH_LONG);
    }
}